package com.example.demo2.controller;

import com.example.demo2.mapper.OrdersMapper;
import com.example.demo2.mapper.SampleMapper;
import com.example.demo2.util.ExcelExportUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private SampleMapper sampleMapper;

    // 获取所有状态为“计划购入”的设备方案
    @Transactional
    @GetMapping("/pending")
    public List<Map<String, Object>> getOrdersByStatus() {
        return ordersMapper.getOrdersByStatus();
    }


    // 更新设备状态为“购置完成”
    @PutMapping("/{id}/complete")
    public String completeOrder(@PathVariable("id") int id) {
        ordersMapper.updateOrderToCompleted(id);
        return "Order status updated to completed";
    }


    // 获取所有状态为“购置完成”的订单
    @GetMapping("/completed")
    public List<Map<String, Object>> getCompletedOrders() {
        return ordersMapper.getCompletedOrders();
    }

    // 删除指定订单
    @DeleteMapping("/{id}")
    public String deleteOrder(@PathVariable("id") int id) {
        ordersMapper.deleteOrderDetailByOrderId(id);
        ordersMapper.deleteOrderById(id);
        return "Order deleted successfully";
    }

    // 批量删除订单
    @DeleteMapping("/batch")
    public String deleteOrdersByIds(@RequestBody List<Integer> ids) {

        ordersMapper.deleteOrdersDetailByIds(ids);

        ordersMapper.deleteOrdersByIds(ids);
        return "Orders deleted successfully";
    }

    // 获取所有状态为“未发放”的样品
    @Transactional
    @GetMapping("/samples/pending")
    public List<Map<String, Object>> getPendingSamples() {
        return sampleMapper.getPendingSamples();
    }

    // 获取所有状态为“已发放”的样品
    @GetMapping("/samples/completed")
    public List<Map<String, Object>> getCompletedSamples() {
        return sampleMapper.getCompletedSamples();
    }

    // 更新样品状态为“已发放”
    @PutMapping("/samples/{id}/complete")
    public String completeSample(@PathVariable("id") int id) {
        sampleMapper.updateSampleStatusToCompleted(id);
        return "Sample status updated to completed";
    }

    // 删除指定样品
    @DeleteMapping("/samples/{id}")
    public String deleteSample(@PathVariable("id") int id) {
        sampleMapper.deleteSampleById(id);
        return "Sample deleted successfully";
    }


    // 批量删除样品清单
    @DeleteMapping("/batchDelete")
    public String deleteSamples(@RequestBody List<Integer> spIds) {
        sampleMapper.deleteSamplesByIds(spIds);
        return "Samples deleted successfully";
    }

    // 导出未发放的样品
    @GetMapping("/exportPendingSamples")
    public void exportPendingSamples(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> pendingSamples = sampleMapper.getPendingSamples();
        String[] headers = {"序号", "标准名称", "标准编号（含年号）", "样品名称", "生产厂家", "规格型号", "申请需求", "状态"};

        byte[] excelData = ExcelExportUtil.exportToExcel("Pending Samples", headers, pendingSamples);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=pending_samples.xlsx");
        response.getOutputStream().write(excelData);
    }

    // 导出已发放的样品
    @GetMapping("/exportCompletedSamples")
    public void exportCompletedSamples(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> completedSamples = sampleMapper.getCompletedSamples();
        String[] headers = {"序号", "标准名称", "标准编号（含年号）", "样品名称", "生产厂家", "规格型号", "申请需求", "状态"};

        byte[] excelData = ExcelExportUtil.exportToExcel("Completed Samples", headers, completedSamples);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=completed_samples.xlsx");
        response.getOutputStream().write(excelData);
    }


    // 导出计划购入的设备方案
    @GetMapping("/exportPendingOrders")
    public void exportPendingOrders(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> pendingOrders = ordersMapper.getOrdersByStatus();
        String[] headers = {"序号", "标准名称", "项目名称", "标准编号及条款号", "设备名称", "解决方案", "状态"};

        byte[] excelData = ExcelExportUtil.exportToExcel("Pending Orders", headers, pendingOrders);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=pending_orders.xlsx");
        response.getOutputStream().write(excelData);
    }

    // 导出已购置完成的设备方案
    @GetMapping("/exportCompletedOrders")
    public void exportCompletedOrders(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> completedOrders = ordersMapper.getCompletedOrders();
        String[] headers = {"序号", "标准名称", "项目名称", "标准编号及条款号", "设备名称", "解决方案", "状态", "oEndDate"};

        byte[] excelData = ExcelExportUtil.exportToExcel("Completed Orders", headers, completedOrders);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=completed_orders.xlsx");
        response.getOutputStream().write(excelData);
    }

}
